#include "seedseditdialog.h"
#include "dbutil.h"
#include<QDateTime>
#include<QMessageBox>
#include <paramdialog.h>
SeedsEditDialog::SeedsEditDialog(QWidget *parent, int seedId) :
    QDialog(parent), seedDialog(new Seeds_Dialog())
{
    seedDialog->setupUi(this);
    this->setStyleSheet("QDialog#Dialog{border-image:url(images/bgd.png)}");
    this->seedId = seedId;
    initSeed();
}
SeedsEditDialog::~SeedsEditDialog()
{
    delete seedDialog;
}

void SeedsEditDialog::on_modify_clicked()
{
    seed.setValidName(seedDialog->validName->text());
    seed.setAuthPass(seedDialog->validPass->text());
    seed.setPriPass(seedDialog->password1->text());
    seed.setPort(seedDialog->snmpPort->text().toInt());
    seed.setVersion(getVersion(seedDialog->snmpVersion->currentIndex()));
    seed.setReadVisitor(seedDialog->readVisit->text());
    seed.setWriteVisitor(seedDialog->writeVisit->text());
    seed.setIp(seedDialog->ip1->text());
    seed.setAuthType(seedDialog->authType->currentIndex());
    seed.setPassType(seedDialog->priType->currentIndex());
    seed.setUpdateTime(QDateTime::currentDateTime());
    seed.setMask(seedDialog->maskEdit->text());
    SeedsDao dao;
    if (seed.getId() == 0)
    {
        seed.setCreateTime(QDateTime::currentDateTime());
        bool result = dao.insertSeeds(seed);
        if (result)
        {
            DbUtils::showMsg(DbUtils::Info_LEVEL, DbUtils::getUtf8String("modify"));
        }
        else
        {
            DbUtils::showMsg(DbUtils::Warn_LEVEL, DbUtils::getUtf8String("modify"));
        }
    }
    else
    {
        bool result = dao.updateSeeds(seed);
        if (result)
        {
            DbUtils::showMsg(DbUtils::Info_LEVEL, DbUtils::getUtf8String("modify"));
        }
        else
        {
            DbUtils::showMsg(DbUtils::Warn_LEVEL, DbUtils::getUtf8String("modify"));
        }
    }
    this->close();
    ParamDialog *pr = static_cast<ParamDialog *>(this->parentWidget());
    pr->initSeeds();
}

void SeedsEditDialog::on_close_clicked()
{
    this->close();
}
void SeedsEditDialog::initSeed()
{
    if (seedId != -1)
    {
        SeedsDao dao;
        this->seed = dao.getSeedsById(seedId);
        seedDialog->validName->setText(seed.getValidName());
        seedDialog->validPass->setText(seed.getAuthPass());
        seedDialog->password1->setText(seed.getPriPass());
        seedDialog->snmpPort->setText(DbUtils::getStringFormInt(seed.getPort()));
        seedDialog->maskEdit->setText(seed.getMask());
        if (seed.getVersion() == 3)
        {
            seedDialog->snmpVersion->setCurrentIndex(2);
        }
        else
        {
            seedDialog->snmpVersion->setCurrentIndex(seed.getVersion());
        }
        seedDialog->readVisit->setText(seed.getReadVisitor());
        seedDialog->writeVisit->setText(seed.getWriteVisitor());
        seedDialog->ip1->setText(seed.getIp());
        seedDialog->authType->setCurrentIndex(seed.getAuthType());
        seedDialog->priType->setCurrentIndex(seed.getPassType());
    }
    else
    {
        seed.setId(0);
    }
}
int SeedsEditDialog::getVersion(int index)
{
    if (index == 0)
    {
        return 0;
    }
    else if (index == 1)
    {
        return 1;
    }
    return 3;
}
int SeedsEditDialog::getIndex(int version)
{
    if (version == 0)
    {
        return 0;
    }
    else if (version == 1)
    {
        return 1;
    }
    return 2;
}
